package com.s4j.struts.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.hibernate.CommonDao;
import com.s4j.struts.biz.model.BizCpAnswer;
import com.s4j.struts.biz.model.BizCpAppr;
import com.s4j.struts.biz.model.BizCpSupplement;
import com.s4j.struts.biz.model.BizLiAppr;
import com.s4j.struts.biz.model.PatientDoc;

@Service
public class CokicpolypDocService {

	@Resource
	private CommonDao dao;
	
	public DatagridPaging findUserByPaging(Integer page, Integer rows,
			PatientDoc patientDoc) {
		List param = new ArrayList();
		String hql = "from PatientDoc p where p.diseaseType = ? ";
		param.add(Constant.PATIENTDOC_DISEASETYPE_CP);
		if(patientDoc != null && !StringUtils.isNullOrBlank(patientDoc.getName())) {
			hql += " and p.name like ?";
			param.add('%' + patientDoc.getName() + '%');
		}
		param.add(page);
		param.add(rows);
		hql += " order by docId asc";
		DatagridPaging users = dao.findByHQLPaging(hql, param.toArray());
		return users;
	}
	
	public void saveCommon(PatientDoc patientDoc) {
		dao.save(patientDoc);
		patientDoc.setDocNo(String.valueOf(patientDoc.getDocId()));
		dao.update(patientDoc);
	}

	public void updateCommon(PatientDoc patientDoc) {
		dao.update(patientDoc);
	}

	public PatientDoc getPatientDoc(Long docId) {
		return dao.get(PatientDoc.class, docId);
	}
	
	public BizCpAppr getCpApprByPatientId(Long docId) {
		String hql = "from BizCpAppr t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizCpAppr.class, docId);
	}
	
	public BizCpSupplement getCpSupplementByPatientId(Long docId) {
		String hql = "from BizCpSupplement t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizCpSupplement.class, docId);
	}
	
	public BizCpAnswer getCpAnswerByPatientId(Long docId) {
		String hql = "from BizCpAnswer t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizCpAnswer.class, docId);
	}
	
	public void save(Object obj) {
		dao.save(obj);
	}
	
	public <T> T get(Class<T> clazz, Long id) {
		return dao.get(clazz, id);
	}

	public void update(Object obj) {
		dao.update(obj);
	}
}
